<template>
  <div>
    <el-dialog
      :title="form.id ? '编辑角色' : '添加角色'"
      :visible.sync="isshow"
      @close="reset"
    >
      <el-form :model="form" :rules="rules" ref="roleForm">
        <el-form-item
          label="角色名称"
          :label-width="formLabelWidth"
          prop="rolename"
        >
          <el-input
            clearable
            v-model="form.rolename"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色权限" :label-width="formLabelWidth">
          <el-tree
            ref="tree"
            :check-strictly="true"
            :data="menuList"
            show-checkbox
            node-key="id"
            :props="defaultProps"
            default-expand-all
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth" prop="status">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="reset">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { roleinfo, menuaddedit } from "../../request/api";
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      isshow: false,
      formLabelWidth: "120px",
      form: {
        rolename: "",
        menus: "",
        status: 2,
      },
      defaultProps: {
        children: "children",
        label: "title",
      },
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    if (this.menuList.length == 0) {
      this.getMenuList();
    }
  },
  computed: {
    ...mapState("menu", ["menuList"]),
  },
  methods: {
    ...mapActions("role", ["menuAddEdit", "getRolelist"]),
    ...mapActions("menu", ["getMenuList"]),
    submitForm() {
      this.$refs.roleForm.validate((val) => {
        if (!val) {
          return;
        }
        let url = this.form.id ? "/roleedit" : "/roleadd";
        this.form.menus = this.$refs.tree.getCheckedKeys().join();
        menuaddedit(url, this.form).then((res) => {
          if (res.code == 200) {
            this.$message.success(res.msg);
            this.getRolelist();
            this.reset();
            return;
          }
          this.$message.error(res.msg);
        });
      });
    },
    getInfo(id) {
      this.isshow = true;
      roleinfo({ id }).then((res) => {
        if (res.code == 200) {
          this.form = res.list;
          this.form.id = id;
          this.$refs.tree.setCheckedKeys(res.list.menus.split(","));
        }
      });
    },
    reset() {
      this.isshow = false;
      this.$refs.roleForm.resetFields();
      this.$refs.tree.setCheckedKeys([]);
      this.form = {
        rolename: "",
        menus: "",
        status: 2,
      };
    },
  },
};
</script>

<style scoped>
</style>